package com.powermanager.adapter;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.powermanager.R;
import com.powermanager.activity.TimeTypeActivity;
import com.powermanager.bean.DayBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by lqy on 16/7/6.
 */
public class TimeTypeAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<DayBean> lists  = new ArrayList<>();
    private ArrayList<DayBean> selected = new ArrayList<>();
    public TimeTypeAdapter(Context context){
        this.context = context;
        lists.add(new DayBean(1,"周一"));
        lists.add(new DayBean(2,"周二"));
        lists.add(new DayBean(3,"周三"));
        lists.add(new DayBean(4,"周四"));
        lists.add(new DayBean(5,"周五"));
        lists.add(new DayBean(6,"周六"));
        lists.add(new DayBean(7,"周天"));
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_timetype,parent,false));
        return viewHolder;
    }

    public ArrayList<DayBean> getSelected() {

        return selected;
    }
    public ArrayList<DayBean> getSortedResult(){
        DayComparator dayComparator = new DayComparator();
        Collections.sort(selected,dayComparator);
        return  selected;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.mTvText.setText(lists.get(position).name);
        if (selected.contains(lists.get(position))){
            myViewHolder.mIvTag.setSelected(true);
        }else
        myViewHolder.mIvTag.setSelected(false);

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((TimeTypeActivity)context).isHead){
                    selected.clear();
                }
                ((TimeTypeActivity)context).resetList();
                if (myViewHolder.mIvTag.isSelected()){
                    myViewHolder.mIvTag.setSelected(false);
                    if (selected.contains(lists.get(position)))
                    selected.remove(lists.get(position));
                }else if (!myViewHolder.mIvTag.isSelected()){
                    myViewHolder.mIvTag.setSelected(true);
                    if (!selected.contains(lists.get(position)))
                    selected.add(lists.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        private View itemView;
        private TextView mTvText;
        private ImageView mIvTag;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            mTvText = (TextView) itemView.findViewById(R.id.mTvText);
            mIvTag = (ImageView) itemView.findViewById(R.id.mIvTag);
        }
    }
    class DayComparator implements Comparator{
        @Override
        public int compare(Object lhs, Object rhs) {
            DayBean dayBean1 = (DayBean) lhs;
            DayBean dayBean2 = (DayBean) rhs;
            int flag = (dayBean1.tag+"").compareTo(dayBean2.tag+"");
            return flag;
        }
    }
}
